package reasoner.care;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import option.GlobalVar;
import option.InitParam;
import option.KBVocabulary;

import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import owl2.convert.KBTransformation;
import preprocessor.absorption.extended.AssertionConversion;
import preprocessor.absorption.extended.OptimizedRestrictions;
import preprocessor.absorption.extended.OptimizedUniversals;

import domain.tc.InitDomainGraph;

import kb.ABox;
import kb.DescriptionLogicsKB;
import kb.TBox;

import reasoner.KBReason;
import tableaux.Completion;
import tableaux.DerivativeConcept;
import utils.ExecTime;
import utils.Util;
import utils.parser.Creator;
import utils.parser.Parser;
import utils.exception.ConceptException;
import utils.exception.CoreReasoningException;
import utils.exception.OrderingDescriptionException;
import utils.exception.SyntaxParsingException;

import algebra.cost.PlanEnumeration;
import algebra.op.ConstantOP;
import algebra.op.JoinOP;
import algebra.op.PrimaryOP;
import algebra.op.ProjectionOP;
import algebra.op.AlgebraicOP;
import algebra.op.ScanOP;
import algebra.op.SelectionOP;
import algebra.query.ObjectQuery;
import algebra.query.indexing.CachedIndex;
import algebra.query.indexing.SecondaryIndex;
import algebra.query.pd.ProjDesc;
import algebra.visitors.AlgebraicOPEvaluator;
import algebra.visitors.cursors.CACursor;
import utils.io.KBAccess;
import utils.io.LoadWebFile;
import entities.Individual;
import entities.assertion.ConceptAssertion;
import entities.axiom.Axiom;
import entities.concept.AndConcept;
import entities.concept.Concept;
import entities.concept.DomainConceptEQ;
import entities.concept.NotConcept;
import entities.role.Role;

public class TestEval {

	public static int countOfTests = 0;
	public static long countOfOrExpansions = 0;
	public static long countOfOrExpansionsTime = 0;
	public static long countOfAndExpansions = 0;
	public static long countOfAndExpansionsTime = 0;
	public static int numOfInst = 0;

	/**
	 * @param args
	 * @throws SyntaxParsingException
	 * @throws CoreReasoningException 
	 */
	public static void main(String[] args) throws SyntaxParsingException, CoreReasoningException {
		String cs1 = "(Digital_SLR_mirrorless)";
		String pd = "(pd-concept top)";
		String prefix = InitParam.getProperty("dir.kb.path");
		 String name = "samplekb.kb";
		String fileloc = prefix + name;
		DescriptionLogicsKB kb = null;
		boolean detailResult = true;
		if (kb == null) {
			try {
				kb = KBAccess.loadKB(fileloc);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ObjectQuery q = ObjectQuery.getObjectQuery(cs1, pd, kb);
		Set<CachedIndex> sis = null;
		String indexLocation = InitParam.getProperty("dir.indices.path");
		try {
			ExecTime et = ExecTime.getStarted();
			sis = CachedIndex.readAllMaterilaizedIndices(kb, indexLocation);
			System.err.println("Time to load indices: "+et.getTime()/1000.0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		AlgebraicOP cand = getLeastCostPlan(q, kb);
		ExecTime timer = ExecTime.getStarted();
		List<ConceptAssertion> ans = CACursor.getResultSet(cand);
		double runtime = timer.getTime() / 1000.0;
		System.out.println("Runtime: " + runtime + "; answer size: "
				+ ans.size() + "\n===========");
	}

	public static AlgebraicOP getLeastCostPlan(ObjectQuery q, DescriptionLogicsKB kb) {
		PlanEnumeration pe = new PlanEnumeration(q, kb);
		long st = System.currentTimeMillis();
		AlgebraicOP finalplan = (AlgebraicOP) pe.systemSelectedPlan()[0];
		System.out.println("Plan obtained: " + finalplan);
		System.out.println("Time spent in optimization (sec):"
				+ (System.currentTimeMillis() - st) * 1.0 / 1000);
		return finalplan;
	}
	
	
	private static String[] analyzeResults(List<ConceptAssertion> res) {
		String[] rets = new String[res.size()];
		// String ret = "";
		int i = 0;
		if (res.size() < 1) {
			System.err.println("\nNo results returned.");
			return rets;
		}
		for (ConceptAssertion ca : res) {
			String ret = "\n";
			ret += ca.getIndividual().toString() + ": \n";
			Concept c = ca.getConcept();
			if (c instanceof AndConcept) {
				AndConcept and = (AndConcept) c;
				for (Concept cc : and.getConceptSet()) {
					// ret += "\t";
					if (cc instanceof AndConcept) {
						for (Concept dd : ((AndConcept) cc).getConceptSet()) {
							ret += dd.toString() + "\n";
						}
					} else
						ret += cc.toString() + " \n";
				}
				ret = ret.substring(0, ret.length() - 1);
			} else {
				ret += c.toString();
			}
			rets[i++] = ret;
			System.out.println(ret + "\n");
		}
		return rets;
	}

}
